<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="80vw"
			   destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false"
			   :close-on-press-escape="mode != 'show' ? false : true">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" label-width="110" ref="dialogForm">
			<el-row :gutter="1">
				<el-col :span="12">
					<el-form-item label="任务名称" prop="title">
						<el-input v-model="form.title" placeholder="请输入任务名称" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="任务执行人" prop="executor_staff">
						<el-select v-model="form.executor_staff" placeholder="请选择" filterable multiple clearable>
							<el-option v-for="item in adminList" :key="item.id" :label="item.name" :value="item.id"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="任务抄送人" prop="send_staff">
						<el-select v-model="form.send_staff" filterable clearable placeholder="请选择" style="width:100%" multiple>
							<el-option v-for="item in adminList" :key="item.id" :label="item.name" :value="item.id"/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="6">
					<el-form-item label="要求完成时间" prop="complete_date">
						<el-date-picker v-model="form.complete_date" type="date" placeholder="请选择日期" :value-format="'YYYY-MM-DD'" style="width:100%"/>
					</el-form-item>
				</el-col>
				<el-col :span="6">
					<el-form-item label="是否推送" prop="is_push">
						<el-switch v-model="form.is_push" :active-value="1" :inactive-value="0"></el-switch>
					</el-form-item>
				</el-col>
				<el-col :span="6">
					<el-form-item label="所属项目" prop="project_id">
						<el-select v-model="form.project_id" placeholder="请选择" filterable clearable>
							<el-option v-for="item in projectList" :key="item.id" :label="item.name" :value="item.id"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="6">
					<el-form-item label="紧急状态" prop="level">
						<el-select v-model="form.level" placeholder="请选择" style="width:100%">
							<el-option label="延迟" :value="3"/>
							<el-option label="正常" :value="2"/>
							<el-option label="加急" :value="1"/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="6">
					<el-form-item label="排序" prop="sort">
						<el-input-number v-model="form.sort" controls-position="right" :min="0" style="width: 100%;"></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :span="6">
					<el-form-item label="是否禁用" prop="system_disable">
						<el-switch v-model="form.system_disable" :active-value="1" :inactive-value="0"></el-switch>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="1">
				<el-col :span="24">
					<sc-editor  v-model="form.content" placeholder="请输入" :height="500"></sc-editor>
				</el-col>
			</el-row>
			<el-row :gutter="1">
				<el-col :span="24">
					<el-form-item label="任务截图" prop="images">
						<sc-upload-affix @success="form.images = form.images+','+$event.src"></sc-upload-affix>
						<sc-upload-multiple v-model="form.images" draggable :limit="1000" :width="80" :height="80"></sc-upload-multiple>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit">保存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import { defineAsyncComponent } from 'vue';
const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
export default {
	components: {
		scEditor
	},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增任务',
				edit: '编辑任务',
				show: '查看任务',
			},
			visible: false,
			isSaveing: false,
			form: {
				title: "",
				complete_date: this.moment().format('YYYY-MM-DD'),
				executor_staff: "",
				send_staff: "",
				level: 2,
				system_disable:0,
				is_push:0,
				content:'',
				images:'',
				project_id:'',
				sort:0
			},
			rules: {
				title: [
					{required: true, message: '请输入任务名称'}
				],
				// executor_staff: [
				// 	{required: true, message: '请选择任务执行人'}
				// ],
				send_staff: [
					{required: true, message: '请选择任务抄送人'}
				],
				complete_date: [
					{required: true, message: '请选择要求完成时间'}
				],
				content: [
					{required: true, message: '请输入任务内容'}
				],
			},
			adminList: [],
			projectList:[]
		}
	},
	mounted() {
		this.getUser()
		this.getProject()
	},
	methods: {
		/**获取员工*/
		async getUser() {
			var res = await this.$API.system.admin.all.get();
			this.adminList = res.data;
		},
		/**获取所属项目*/
		async getProject() {
			var res = await this.$API.basic_new.projeck.all.get();
			this.projectList = res.data;
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.task.edit.post(this.form);
					} else {
						res = await this.$API.task.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.visible = false;
						this.$emit('success', this.form, this.mode)
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		//表单注入数据
		async setData(data) {
			this.form.id = data.id
			var res = await this.$API.task.detail.get({id: data.id});
			Object.assign(this.form, res.data)
		},

	}
}
</script>

<style>
</style>
